
## 2012
cces = fread('https://dataverse.harvard.edu/api/access/datafile/:persistentId?persistentId=doi:10.7910/DVN/HQEVPK/J0KBXB', quote = '')

# obama voter
cces[, obavot := ifelse(CC354b==2 | CC354c==2 | CC410a==1 | CC410a_nv==1, 1, 0)] 
# obama voter and pres donor
cces[, obadon := ifelse(obavot==1 & CC417bx_1==1, 1, 0)]

# romney voter
cces[, romvot := ifelse(CC354b==1 | CC354c==1 | CC410a==2 | CC410a_nv==2, 1, 0)]

# romney voter and pres donor
cces[, romdon := ifelse(romvot==1 & CC417bx_1==1, 1, 0)]

# Now get bottom 90% income: for 2012 that is less than 120K (or less than 13 on the scale)
# quantile(cces[faminc < 19]$faminc, probs = seq(0, 1, 0.05), na.rm = TRUE)

# So bottom 90 is less than 11 (<120K is last group wholly in bottom 90)
cces[, bot90 := ifelse(faminc < 11, 1, 0)]

# Bottom 50 is less than 6 (<50K is last group wholly in bot 50)
cces[, bot50 := ifelse(faminc < 6, 1, 0)]

# Just pull those donors from the data:
obdons = cces[obadon == 1]
romdons = cces[romdon == 1]

## 2016 
cces = fread('https://dataverse.harvard.edu/api/access/datafile/:persistentId?persistentId=doi:10.7910/DVN/GDF6Z0/JPMOZZ', quote = '')

# Trump voter
cces[, truvot := ifelse(CC16_364b==1 | CC16_364c==1 | CC16_410a==1 | CC16_410a_nv==1, 1, 0)]

# Trump voter and pres donor  
cces[, trudon := ifelse(truvot==1 & CC16_417bx_1==1, 1, 0)]

# Clinton voter
cces[, clivot := ifelse(CC16_364b==2 | CC16_364c==2 | CC16_410a==2 | CC16_410a_nv==2, 1, 0) ]

# Clinton voter and pres donor
cces[, clidon := ifelse(clivot==1 & CC16_417bx_1==1, 1, 0)]

# Now get bottom 90% income
# quantile(cces[faminc < 17]$faminc,probs = seq(0, 1, 0.05), na.rm = TRUE)

#So here bottom 90 is less than 11 (<120K is last group wholly in bottom 90)
cces[, bot90 := ifelse(faminc < 11, 1, 0)]

#Bottom 50 is less than 6 (<50K is last group wholly in bot 50)
cces[, bot50 := ifelse(faminc < 6, 1, 0)]

trudons16 = cces[trudon == 1]
clidons = cces[clidon == 1]

## 2020

cces = fread('https://dataverse.harvard.edu/api/access/datafile/4949558')

# Trump voter
cces[, truvot := ifelse(CC20_364a==1 | CC20_364b==1 | CC20_410==2 | CC20_410a_nv==2, 1, 0)]

# Trump voter and pres donor
cces[, trudon := ifelse(truvot==1 & CC20_430b_1==1, 1, 0)]

# Biden voter
cces[, bidvot := ifelse(CC20_364a==2 | CC20_364b==2 | CC20_410==1 | CC20_410a_nv==1, 1, 0) ]

# Biden voter and pres donor
cces[, biddon := ifelse(bidvot==1 & CC20_430b_1==1, 1, 0)]

# Now get bottom 90% income
# quantile(cces[faminc_new < 17]$faminc_new,probs = seq(0, 1, 0.05), na.rm = TRUE)

# So here bottom 90 is less than 11 (<120K is last group wholly in bottom 90)
cces[, bot90 := ifelse(faminc_new < 11,1,0)]

#Bottom 50 is less than 6 (<50K is last group wholly in bot 50)
cces[, bot50 := ifelse(faminc_new < 6,1,0)]

trudons20 = cces[trudon == 1]
biddons = cces[biddon == 1]

rm(cces)

## So let's pull each of these together on education, race, sex

obdons = obdons[, .(weight = V103, gender, race, educ, bot90, bot50, faminc)]
romdons = romdons[, .(weight = V103, gender, race, educ, bot90, bot50, faminc)]
clidons = clidons[, .(weight = commonweight, gender, race, educ, bot90, bot50, faminc)]
trudons16 = trudons16[, .(weight = commonweight, gender, race, educ, bot90, bot50, faminc)]
biddons = biddons[, .(weight = commonweight, gender, race, educ, bot90, bot50, faminc = faminc_new)]
trudons20 = trudons20[, .(weight = commonweight, gender, race, educ, bot90, bot50, faminc = faminc_new)]

donors = rbindlist(list(
  'Obama12' = obdons,
  'Romney12' = romdons,
  'Clinton16' = clidons,
  'Trump16' = trudons16,
  'Biden20' = biddons,
  'Trump20' = trudons20
), idcol = 'donor')

rm(obdons, romdons, clidons, trudons16, biddons, trudons20)
names(donors) = c("donor", "weight", "gender", "race", "education", "bottom90", "bottom50", "income")

donors[, educ2 := fcase(
  education<1.5, "Less than High School",
  education<2.5, "High School Degree",
  education<3.5, "College (No Degree)", 
  education<5.5, "College Degree",
  default = "Post-Graduate"
)]

donors_b90 = donors[bottom90 == 1]

# get percent breakdowns

allatts = donors_b90 %>%
  group_by(donor) %>%
  summarize(
    Male=length(gender[gender==1])/length(gender),
    Female=length(gender[gender==2])/length(gender),
    White=length(race[race==1])/length(race),
    Black=length(race[race==2])/length(race),
    Hispanic=length(race[race==3])/length(race),
    Asian=length(race[race==4])/length(race),
    Other=length(race[race %in% 5:8])/length(race),
    CollegeDegree=length(educ2[educ2 %in% c("College Degree", "Post-Graduate")])/length(educ2),
    NoCollegeDegree=length(educ2[educ2 %in% c("Less than High School", "High School Degree", "College (No Degree)")])/length(educ2),
    CollegeWhite=length(educ2[educ2 %in% c("College Degree", "Post-Graduate") & race == 1])/length(educ2[race==1]),
    NoCollegeWhite=length(educ2[educ2 %in% c("Less than High School", "High School Degree", "College (No Degree)") & race == 1])/length(educ2[race==1])
  )

allatts$num = c(5,3,1,2,4,6)
allatts = allatts %>% arrange(num)
allatts$num = NULL

# transposed
trans = data.frame(t(allatts))
names(trans) = trans[1,]
trans = trans[-1,]
trans = cbind(rownames(trans), trans)

# Make numeric
trans[, 2:7] = apply(trans[, 2:7], 2, \(x) as.numeric(as.character(x)))

names(trans)[1] = "Demographics"

save(trans, file = 'summary_data/tableA10.rda')

